package com.weipu.common.facade.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
import com.weipu.common.constants.Constant;
import com.weipu.common.constants.ServerConstant;
import com.weipu.common.facade.ShoppingCartServiceProvider;
import com.weipu.common.facade.exception.DxException;
import com.weipu.common.facade.model.Lines;
import com.weipu.common.facade.model.Parmars;
import com.weipu.common.facade.model.PreviewOrderModel;
import com.weipu.common.util.JsonFormat;
import com.weipu.common.util.Logger;
import com.weipu.common.util.StringUtil;

public class ShoppingCartServiceProviderImpl extends BaseServiceProviderImpl
		implements ShoppingCartServiceProvider {

	private static Map<String, String> mapUrl;

	static {
		mapUrl = new HashMap<String, String>();
		mapUrl.put(ServerConstant.RequestMethodNameConstant.METHOD_GETCART,
				Constant.GETCART_URL);
		mapUrl.put(ServerConstant.RequestMethodNameConstant.METHOD_ADDCART,
				Constant.ADDCART_URL);
		mapUrl.put(ServerConstant.RequestMethodNameConstant.METHOD_CLEARCART,
				Constant.CLEARCART_URL);
		mapUrl.put(
				ServerConstant.RequestMethodNameConstant.METHOD_REMOVESKUFROMCART,
				Constant.REMOVESKUFROMCART_URL);
		mapUrl.put(
				ServerConstant.RequestMethodNameConstant.METHOD_GETCARTCOUNT,
				Constant.GETCARTCOUNT_URL);
		mapUrl.put(ServerConstant.RequestMethodNameConstant.METHOD_UPDATECART,
				Constant.UPDATECART_URL);
		mapUrl.put(
				ServerConstant.RequestMethodNameConstant.METHOD_UPLOADLOCALCART,
				Constant.UPLOADLOCALCART_URL);
	}

	@Override
	public PreviewOrderModel getCart(long customerId, String token, String notSelectedSkuStr)
			throws DxException {
		// TODO Auto-generated method stub
		PreviewOrderModel previewOrderModel = null;
		String currencyCode = StringUtil.getConfig(
				ServerConstant.Parameter.PARAMETER_NAME_CURRENCYCODE, "USD");

		JSONObject data = requestHttpGetData(
				mapUrl.get(ServerConstant.RequestMethodNameConstant.METHOD_GETCART)
						+ "?customerID=" + customerId + "&Token="
						+ token+"&notSelectedSkuStr="+notSelectedSkuStr,
				currencyCode);
		if (data.optBoolean("IsSuccess")) {
			previewOrderModel = PreviewOrderModel.json2PreviewOrderModel(data
					.optJSONObject("PreviewOrder"));
			
			if(previewOrderModel!=null){
				previewOrderModel.setMerged(data.optBoolean(ServerConstant.PreviewOrder.PreviewOrder_IsMerged));
			}

		}

		return previewOrderModel;
	}

	@Override
	public PreviewOrderModel updateCart(int sku, int quantity, long customerId,
			String token) throws DxException {
		PreviewOrderModel model = null;
		boolean success;
		int count = -1;// 返回的当前购物车中存在的商品品类数量
		String currencyCode = StringUtil.getConfig(
				ServerConstant.Parameter.PARAMETER_NAME_CURRENCYCODE, "USD");
		JSONObject data = requestHttpGetData(
				mapUrl.get(ServerConstant.RequestMethodNameConstant.METHOD_ADDCART)
						+ "?sku="
						+ sku
						+ "&qty="
						+ quantity
						+ "&customerID="
						+ customerId + "&Token=" + token, currencyCode);
		success = data.optBoolean("IsSuccess");
		if (success) {
			model = PreviewOrderModel.json2PreviewOrderModel(data
					.optJSONObject("PreviewOrder"));
		}

		return model;
	}

	@Override
	public void clearCart(long customerId, String token) throws DxException {
		String currencyCode = StringUtil.getConfig(
				ServerConstant.Parameter.PARAMETER_NAME_CURRENCYCODE, "USD");

		requestHttpGetAll(
				mapUrl.get(ServerConstant.RequestMethodNameConstant.METHOD_CLEARCART)
						+ "?customerID=" + customerId + "&Token=" + token,
				currencyCode);
		// JSONObject data = json.optJSONObject(Constant.JSON_DATA);
		// JSONArray js = data.optJSONArray(Constant.JSON_RESULTSET);
	}

	@Override
	public PreviewOrderModel removeSkuFromCart(int sku, long customerId,
											   String token, String notSelectedSkuStr) throws DxException {
		PreviewOrderModel model = null;
		String currencyCode = StringUtil.getConfig(
				ServerConstant.Parameter.PARAMETER_NAME_CURRENCYCODE, "USD");
		JSONObject data = requestHttpGetData(
				mapUrl.get(ServerConstant.RequestMethodNameConstant.METHOD_REMOVESKUFROMCART)
						+ "?sku="
						+ sku
						+"&notSelectedSkuStr="
						+notSelectedSkuStr
						+ "&customerID="
						+ customerId
						+ "&Token=" + token, currencyCode);
		boolean success = data.optBoolean("IsSuccess");
		if (success) {
			model = PreviewOrderModel.json2PreviewOrderModel(data
					.optJSONObject("PreviewOrder"));
		}
		return model;
	}

	@Override
	public int getCartCount(long customerId, String token) throws DxException {
		int count = 0;
		String currencyCode = StringUtil.getConfig(
				ServerConstant.Parameter.PARAMETER_NAME_CURRENCYCODE, "USD");
		JSONObject data = requestHttpGetData(
				mapUrl.get(ServerConstant.RequestMethodNameConstant.METHOD_GETCARTCOUNT)
						+ "?customerID=" + customerId + "&Token=" + token,
				currencyCode);
		count = data.optInt("itemCount");
		return count;
	}

	@Override
	public PreviewOrderModel addCart(int sku, int qty, long customerId,
									 String token, String notSelectedSkuStr) throws DxException {
		PreviewOrderModel model = null;
		String currencyCode = StringUtil.getConfig(
				ServerConstant.Parameter.PARAMETER_NAME_CURRENCYCODE, "USD");
		JSONObject data = requestHttpGetData(
				mapUrl.get(ServerConstant.RequestMethodNameConstant.METHOD_ADDCART)
						+ "?sku="
						+ sku
						+ "&qty="
						+ qty
						+"&notSelectedSkuStr="
						+notSelectedSkuStr
						+ "&customerID="
						+ customerId + "&Token=" + token, currencyCode);
		boolean success = data.optBoolean("IsSuccess");
		if (success) {
			model = PreviewOrderModel.json2PreviewOrderModel(data
					.optJSONObject("PreviewOrder"));
		}

		return model;
	}

	@Override
	public PreviewOrderModel uploadLocalCart(ArrayList<Lines> skus,
			long customerId, String token) throws DxException {
		PreviewOrderModel model = null;
		Parmars parmars = new Parmars();
		parmars.setCustomerID(customerId);
		parmars.setSkus(skus);
		parmars.setToken(token);
		JSONObject data = requestHttpPostData(mapUrl.get(ServerConstant.RequestMethodNameConstant.METHOD_UPLOADLOCALCART),
				parmars,null);
		boolean success = data.optBoolean("IsSuccess");
		if (success) {
			model = PreviewOrderModel.json2PreviewOrderModel(data
					.optJSONObject("PreviewOrder"));
			if(model!=null){
				model.setMerged(data.optBoolean(ServerConstant.PreviewOrder.PreviewOrder_IsMerged));
			}
		}else{
			if(data.optJSONObject("Error")!=null){
				throw new DxException(data.optJSONObject("Error").optString("ErrorCode"));
			}
		}
		return model;
	}

}
